﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using BUS;
using Entity;
using Share;
using DevExpress.XtraGrid.Localization;

namespace SLMNG
{
    public partial class CA_Tables : BaseForm
    {
        #region Form Event

        public CA_Tables()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
            StatusOfForm = FormStatus.Add;
        }        

        void Form1_Load(object sender, EventArgs e)
        {            
            try
            {
                GridLocalizer.Active = new MyGridLocalizer();
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(HandleException.HandleSQLException(ex.Message), this.Text);
            }
        }

        #endregion

        #region Support Methods

        private void LoadData()
        {
            string[] arg = new string[] { "ZoneCode" };
            object[] obj = new object[] { DBNull.Value };
            DataSet ds1 = CatalogueBase.GetData(Zone.STORE_SELECT, arg, obj);
            cmbZone.Properties.DataSource = ds1.Tables[0];

            arg = new string[] { "TableCode", "ZoneCode" };
            obj = new object[] { DBNull.Value, DBNull.Value };
            DataTable dt = CatalogueBase.GetData(Table.STORE_SELECT, arg, obj).Tables[0];
            gridView.DataSource = dt;
        }

        #endregion

        #region Override Methods

        public override void fncInsert()
        {
            tabChildForm.SelectedTabPageIndex = 1;
            ActionOnControl(new Control[] { txtTableCode }, true);
            StatusOfForm = FormStatus.Add;
            ClearForm();
        }

        public override void fncSave()
        {
            if (tabChildForm.SelectedTabPageIndex == 1)
            {
                if (string.IsNullOrEmpty(txtTableCode.Text))
                {
                    MessageBox.Show(temp.ID_IS_NOT_NULL);
                    return;
                }
                else if (string.IsNullOrEmpty(txtTableName.Text))
                {
                    MessageBox.Show(temp.NAME_IS_NOT_NULL);
                    return;
                }

                string[] arg = new string[] { "TableCode", "TableName", "ZoneCode" };
                object[] obj = new object[] { txtTableCode.Text.Trim(), txtTableName.Text.Trim(),
                                                cmbZone.EditValue.ToString()};
                string result = string.Empty;
                if (StatusOfForm == FormStatus.Add)
                    result = CatalogueBase.SaveData(Table.STORE_INSERT, arg, obj);
                else if (StatusOfForm == FormStatus.Edit)
                    result = CatalogueBase.SaveData(Table.STORE_UPDATE, arg, obj);

                if (result == "0")
                {
                    if (StatusOfForm == FormStatus.Add)
                    {
                        ClearForm();
                        MessageBox.Show(temp.STATUS_INSERT_SUCCESS, this.Text);
                    }
                    else
                        MessageBox.Show(temp.STATUS_UPDATE_SUCCESS, this.Text);
                }
                else
                    MessageBox.Show(HandleException.HandleSQLException(result), this.Text);

            }
        }

        private void ClearForm()
        {
            txtTableCode.Text = string.Empty;
            txtTableName.Text = string.Empty;
            txtTableCode.Focus();
        }

        public override void fncEdit()
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                tabChildForm.SelectedTabPageIndex = 1;
                ActionOnControl(new Control[] { txtTableCode }, false);
                StatusOfForm = FormStatus.Edit;

                string[] arg = new string[] { "TableCode","ZoneCode" };
                object[] obj = new object[] { gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "TableCode").ToString(), DBNull.Value };
                DataTable dt = CatalogueBase.GetData(Table.STORE_SELECT, arg, obj).Tables[0];
                txtTableCode.Text = dt.Rows[0]["TableCode"].ToString();
                txtTableName.Text = dt.Rows[0]["TableName"].ToString();
                cmbZone.EditValue = dt.Rows[0]["ZoneCode"];
            }
        }

        public override void fncDelete()
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                MessageBox.Show(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ZoneCode").ToString());
            }
        }

        public override void fncRefresh()
        {
            LoadData();
        }

        #endregion

    }
}
